# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from ..items import WxappItem

class WxappSpiderSpider(CrawlSpider):
    name = 'wxapp_spider'
    allowed_domains = ['wxapp-union.com']
    start_urls = ['http://www.wxapp-union.com/portal.php?mod=list&catid=2&page=1']

    rules = (
        #allow之内是符合正则表达式的url就进行爬取，而follow为是否跟进,callback回调函数为进行页面解析
                Rule(LinkExtractor(allow=r'.+mod=list&catid=2&page=\d'), follow=True),
        Rule(LinkExtractor(allow=r'.+article-.+\.html'),callback="parse_detail",follow=False)
    )

    def parse_detail(self, response):
        html=response
        title=html.xpath('//h1[@class="ph"]/text()')[0].get()
        auth=html.xpath('//p[@class="authors"]/a/text()')[0].get()
        puptime=html.xpath('//span[@class="time"]/text()')[0].get()
        item=WxappItem(title=title,author=auth,puptime=puptime)
        yield item